<?php


class ObservationdepthTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('Observationdepth');
    }

    /**
     * Override 
     * Create an object with values provided $params
     * @param array $params
     */
    public function create(array $params = array()) {
        $params['unitid'] = Doctrine::getTable('Unit')->findOneBy('unit', 'm')->id;

        return parent::create($params);
    }


    /**
     * Find the first depth ordered by depth value
     * @param string $where         Query WHERE part
     * @param array $param          an array of parameters or a simple scalar
     * @return mixed                Doctrine_Record or false if no result.
     */
    public function findFirstDepth($where, $param) {
        return $this->createQuery('od')
                ->where($where, $param)
                ->orderBy('observationdepth asc')
                ->limit(1)
                ->fetchOne();
    }

    /**
     * Find the first depth ordered by depth id
     * @param string $where         Query WHERE part
     * @param array $param          an array of parameters or a simple scalar
     * @return mixed                Doctrine_Record or false if no result.
     */
    public function findLatestDepth($where, $param) {
        return $this->createQuery('od')
                ->where($where, $param)
                ->orderBy('id desc')
                ->limit(1)
                ->fetchOne();
    }

    public function deleteById($id) {

        if ($this->isSaveToDelete($id)) {

            // return affected rows
            return $this->createQuery('od')
                    ->delete()
                    ->where('id = ?', $id)
                    ->execute();
        }
        else {
            return false;
        }
    }

    /**
     * Do this check to prevent cascade delete
     * @param int $id
     * @return boolean
     */
    public function isSaveToDelete($id){
        $details = Doctrine::getTable('Observationdepthdetail')
                    ->findOneBy('observationdepthid', $id);
        $attributes = Doctrine::getTable('Observationdepthattribute')
                    ->findOneBy('observationdepthid', $id);
        
//        sfContext::getInstance()->getLogger()->debug('count: '. $count->count());

        return !($details || $attributes);
    }

    /**
     * find a depth before or after depth $depthId
     * within an observation location $observationId
     * @param int $depthId
     * @param int $observationId
     * @return Doctrine_Record|boolean
     */
    public function findOneBeforeOrAfter($depthId, $observationId)
    {

        // find 'before' first
        $result = $this->createQuery('od')
                ->where('id < ?', $depthId)
                ->andWhere('observationid = ?', $observationId)
                ->limit(1)
                ->fetchOne();

        if ($result)
        {
            return $result;
        }

        // if 'before' is not found, then find 'next'
        $result = $this->createQuery('od')
                ->where('id > ?', $depthId)
                ->andWhere('observationid = ?', $observationId)
                ->limit(1)
                ->fetchOne();
        
        if ($result)
        {
            sfContext::getInstance()->getLogger()->debug('depth after: ' . $result->observationdepth);
            return $result;
        }
        
        // otherwise, this observation has empty depth
        return false;
    }

}